#ifndef QT_EXT_QTREE_H
#define QT_EXT_QTREE_H

#include "QVector"

template <class T> 
class QTreeNode
{
    
public :
    T  m_data;
    QTreeNode * m_parent;
    QVector<QTreeNode<T>*> * m_children;

    QTreeNode(){}
    QTreeNode(T node):m_parent(0) ,m_children(0)
    {
        m_data = node;
       // m_children = new QVector<QTreeNode <T>*>();
    }
    
    void addChild(QTreeNode * node)
    {
        if (m_children == 0)
        {
            m_children = new QVector<QTreeNode <T>*>();
        }
        m_children->append(node);
        node->m_parent = this;
    }
    
    void release()
    {
        //delete m_node;
    }
    
    void removeFromParent()
    {
        release();
        if (m_parent == 0) 
        {
            delete this;
            return;
        }
        if (m_parent->m_children == 0)return;
        for (int i =0 ; i <m_parent->m_children->size(); i++)
        {
           QTreeNode<T> * node =  m_parent->m_children->at(i);
           if (node == this)
           {
               m_parent->m_children->remove(i);
               delete this;
               return;
           }
        }
        
    }

    
    QVector<T> getSupertypes()
    {
        QVector<T> vector;
        vector.append(this->m_data);
		QTreeNode<T> * parentNode = this;
        while (true )
        {
            parentNode = parentNode->getParent();
            if (parentNode == 0)
            {
                return vector;
            }
            vector.append(parentNode->m_data);

        }
    }
    
    void removeChildren()
    {
		if (!hasChildren()) return;
        for (int i =0 ; i <m_children->size(); i++)
        {
           QTreeNode<T> * node =  m_children->at(i);
           node->removeChildren();
        }
        delete m_children;
    }

    
    QTreeNode<T> * getParent()
    {
        return m_parent;
    }

    QVector<QTreeNode<T>*> * getChildren()
    {
        return m_children;
    }
    
    bool hasChildren()
    {
        if (m_children == NULL || m_children->size() == 0)
			 return false;
		return true;
    }
};


template <class T>
class QTree
{
    
public:
    QTreeNode<T> * m_root;
    
    QTreeNode<T> *RootNode()
    {
        return m_root;
    }
    
    
    QTree(QTreeNode<T> * root){m_root = root;}
    ~QTree(){
        m_root->removeChildren();
        delete m_root;
    }
    bool isRoot(QTreeNode<T>* node)
    {
        return m_root == node;
    }
    

    
};


#endif
